Skip to content

improve(math): Add fast path for x % 1.0 and x % -1.0 #1447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2020
Merged

improve(math): Add fast path for x % 1.0 and x % -1.0 #1447

merged 2 commits into from
Sep 4, 2020

Conversation

MaxGraey
Copy link
Member

@MaxGraey MaxGraey commented Aug 24, 2020

Fast path:

x % +1.0  ==>
x % -1.0  ==>   copysign(x - trunc(x), x)

It similar what we already do for Math.pow(x, 2.0) and etc.

In future it could be generalized to x % C_pot => copysign(x - C_pot * trunc(x * (1 / C_pot)), x),
where C_pot is pow of two constant.

  • I've read the contributing guidelines

// TODO: move this rule to compiler's optimization pass
if (builtin_abs<f64>(y) == 1.0) {
// x % 1, x % -1 ==> sign(x) * abs(x - trunc(x))
return builtin_copysign<f64>(x - builtin_trunc<f64>(x), x);
Copy link
Member Author

@MaxGraey MaxGraey Aug 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copysign necessary for handling -0.0 % 1.0 -> -0.0 edge case

@MaxGraey
Copy link
Member Author

Actually it could be apply for any pow of two modulo divisor

@MaxGraey MaxGraey requested a review from dcodeIO September 2, 2020 08:35
@dcodeIO dcodeIO merged commit b463346 into AssemblyScript:master Sep 4, 2020
@MaxGraey MaxGraey deleted the mod-fast-path branch September 4, 2020 23:52
@github-actions
Copy link

github-actions bot commented Sep 5, 2020

🎉 This PR is included in version 0.14.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants